
<template>
	<view :class="[config.style]">
		<form @submit="onSubmit">
			<view class="avatar-section">
				<input type="hidden" name="avatar" v-model="avatar" style="display: none;"/>
				<view class="avatar">
					<button class="avatar-btn" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
						<image class="image" :src="avatarUrl"></image>
					</button>
				</view>
			</view>
			<view class="nickname-section">
				<view class="title">昵称</view>
				<input type="nickname" name="nickname" class="uni-input" maxlength="60" placeholder="请输入昵称" v-model="nickname"/>
			</view>
			
			<view class="submit">
				<button class="button" hover-class="button-hover" form-type="submit">确认提交</button>
			</view>
		</form>
	</view>
</template>

<script>
	import { mapState,mapMutations } from 'vuex'
	export default {
		data() {
			return {
				userInfo: {},
				avatar: '', // 头像路径
				avatarUrl: '', // 头像url
				nickname: '', // 昵称
			}
		},
		async onLoad(option) {
			await this.$onLaunched
			await this.getUserInfo()
			this.avatar = this.userInfo.memberInfo.avatar
			this.avatarUrl = this.userInfo.memberInfo.avatar
			this.nickname = this.userInfo.memberInfo.nickname
		},
		computed: {
			...mapState(['config'])
		},
		
		methods: {
			/**
			 * 获取已登录用户数据
			 */
			async getUserInfo() {
				console.log('执行了')
				let that = this
				let userInfo = uni.getStorageSync('userInfo') || {}
				console.log('userInfo',userInfo)
				if(Object.keys(userInfo).length > 0){
					this.userInfo = userInfo
				}else{
					this.userInfo = {}
				}
			},
			
			/**
			 * 获取用户详情数据
			 */
			getApiUserInfo() {
				let that = this
				let data = {}
					data.action = 'user_info'
					data.m = 'muu_classroom'
				//动态获取用户数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/ucenter'
					//#endif
					//#ifdef H5
					url = 'entry/site/ucenter'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.userInfo.sessionid = that.userInfo.sessionid
							that.userInfo.memberInfo = res.data.data
							uni.setStorageSync('userInfo', that.userInfo)
							//console.log(that.userInfo)
						}
					}
				})
			},
			
			onChooseAvatar(e) {
				console.log(e)
				const { avatarUrl } = e.detail
				console.log(avatarUrl)
				this.uploadImgs(avatarUrl)
				//this.avatar = avatarUrl
			},
			
			uploadImgs(filePath) {
				let that = this;
				//#ifdef MP-WEIXIN
				let url = this.util.url('entry/wxapp/file',{
					m: 'muu_classroom',
					action: 'upload'
				})
				//#endif
				uni.uploadFile({
					url: url,
					filePath: filePath,
					fileType: 'image',
					name: 'file',
					header: {
						// #ifdef MP
						"Content-Type": "multipart/form-data",
						// #endif
					},
					success: (res) => {
						uni.hideLoading();
						console.log(res)
						const item = JSON.parse(res.data);
						that.avatar = item.attachment
						that.avatarUrl = item.url
					},
					fail: (err) => {
						uni.showToast({
						    title: '头像上传失败',
						    duration: 2000,
							icon: 'none'
						});
					}
				})
			},
			
			// 头像完成上传
			avatarSubmit() {
				let that = this

				//提交数据
				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/ucenter'
					//#endif
					//#ifdef H5
					url = 'entry/site/ucenter'
					//#endif
				this.util.request({
					url : url,
					data: {
						'action': 'avatar',
						'avatar': that.avatar,
					},
					method: 'POST',
					
					success: function (res) {
						//console.log(res.data);
						if (res.data.code == 200) {
							that.getUserInfo()
						}
						// }else{
						// 	uni.showToast({
						// 	    title: res.data.msg,
						// 	    duration: 2000,
						// 		icon: 'none'
						// 	});
						// }
					}
				})
			},
			
			nicknameSubmit() {
				let that = this

				//提交数据
				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/ucenter'
					//#endif
					//#ifdef H5
					url = 'entry/site/ucenter'
					//#endif
				this.util.request({
					url : url,
					data: {
						'action': 'nickname',
						'nickname': that.nickname,
					},
					method: 'POST',
					
					success: function (res) {
						//console.log(res.data);
						if (res.data.code == 200) {
							that.getUserInfo()
						}
						// }else{
						// 	uni.showToast({
						// 	    title: res.data.msg,
						// 	    duration: 2000,
						// 		icon: 'none'
						// 	});
						// }
					}
				})
			},

			onSubmit(e) {
				console.log(e)
				this.avatar = e.detail.value.avatar
				this.nickname = e.detail.value.nickname
				//console.log(this.avatar)
				//console.log(this.nickname)

				if(this.avatar){
					this.avatarSubmit()
				}
				if(this.nickname){
					this.nicknameSubmit()
				}
				uni.showToast({
				    title: '提交成功',
				    duration: 2000,
					icon: 'none'
				});
				uni.navigateBack({
					delta: 2
				});
			}
		}
	}
</script>

<style lang="scss">
	.avatar-section {
		.avatar {
			padding: 60rpx 0 60rpx 0;
			.avatar-btn {
				border: 0;
				padding: 0;
				width: 120rpx;
				height: 120rpx;
				margin: 0 auto;
				border-radius: 10rpx;
				.image {
					width: 120rpx;
					height: 120rpx;
					border-radius: 10rpx;
				}
			}
		}
	}
	.nickname-section {
		position: relative;
		padding: 20rpx;
		.title {
			position: absolute;
			top: 40rpx;
			left: 20rpx;
		}
		.uni-input {
			padding: 20rpx 0;
			padding-left: 120rpx;
			border-top: 2rpx solid #eee;
			border-bottom: 2rpx solid #eee;
		}
	}
	.submit {
		position: fixed;
		bottom: 20rpx;
		left: 20rpx;
		right: 20rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom));
		padding-bottom: calc(env(safe-area-inset-bottom));
		.button {
			display: block;
			color: #fff;
		}
		.button-hover {
			color: #fff;
		}
	}
	
	.Blue {
		.submit {
			.button {
				background-color: $Blue;
			}
			.button-hover {
				background-color: $Blue-hover;
			}
		}
	}
	.Green {
		.submit {
			.button {
				background-color: $Green;
			}
			.button-hover {
				background-color: $Green-hover;
			}
		}
	}
	.LightRed {
		.submit {
			.button {
				background-color: $LightRed;
			}
			.button-hover {
				background-color: $LightRed-hover;
			}
		}
	}
	.Orange {
		.submit {
			.button {
				background-color: $Orange;
			}
			.button-hover {
				background-color: $Orange-hover;
			}
		}
	}
	.LightPink {
		.submit {
			.button {
				background-color: $LightPink;
			}
			.button-hover {
				background-color: $LightPink-hover;
			}
		}
	}
	.Magenta {
		.submit {
			.button {
				background-color: $Magenta;
			}
			.button-hover {
				background-color: $Magenta-hover;
			}
		}
	}
</style>

